var map;
var gdir;
var gdir2;
var geocoder = null;
var addressMarker;
var html = "";
var distBetweenWeather = 50000;
var infoTable = new Array();;

function initialize() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map_canvas"));
		gdir = new GDirections(map);
		gdir2 = new GDirections(map);

		GEvent.addListener(gdir, "load", onGDirectionsLoad);
		GEvent.addListener(gdir2, "load", onGDirectionsLoad);
		GEvent.addListener(gdir, "error", handleErrors);
		map.addControl(new GLargeMapControl())

		var mapControl = new GHierarchicalMapTypeControl();

		// Set up map type menu relationships
		mapControl.clearRelationships();
		mapControl.addRelationship(G_SATELLITE_MAP, G_HYBRID_MAP, "Labels",
				false);

		// Add control after you've specified the relationships
		map.addControl(mapControl);

		setDirections("Oslo", "Bergen, Norway");// , "no");

	}
}

function setDirections(fromAddress, toAddress) { // ,locale) {
	gdir.load("from: " + fromAddress + " to: " + toAddress, {
		getSteps :true
	});
	// gdir2.load("from: stavanger to: �lesund",{getSteps:true});
	// { "locale": locale } +
}

function changeHTMLContent(html) {
	document.getElementById('content').innerHTML = html;
}

function handleErrors() {
	if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
		alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: "
				+ gdir.getStatus().code);
	else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
		alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: "
				+ gdir.getStatus().code);

	else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
		alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: "
				+ gdir.getStatus().code);

	// else if (gdir.getStatus().code == G_UNAVAILABLE_ADDRESS) <--- Doc bug...
	// this is either not defined, or Doc is wrong
	// alert("The geocode for the given address or the route for the given
	// directions query cannot be returned due to legal or contractual
	// reasons.\n Error code: " + gdir.getStatus().code);

	else if (gdir.getStatus().code == G_GEO_BAD_KEY)
		alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: "
				+ gdir.getStatus().code);

	else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
		alert("A directions request could not be successfully parsed.\n Error code: "
				+ gdir.getStatus().code);

	else
		alert("An unknown error occurred. " + gdir.getStatus().code);

}

// Use this function to access information about the latest load()
// results.
function onGDirectionsLoad() {
	infoTable = new Array();
	var countFindings = 0;
	html = "";

	// Traverserer hver rute
	for ( var i = 0; i < gdir.getNumRoutes(); i++) {
		var groute = gdir.getRoute(i);
		var distanceTravelled = 0;

		// Traverserer hvert steg i ruten
		for ( var j = 0; j < groute.getNumSteps(); j++) {
			// html += "<p>NYTT STEG.<br>";
			var gstep = groute.getStep(j);
			distanceTravelled += gstep.getDistance().meters;
			// html += "Avstand i meter siden forrige måling:" +
			// distanceTravelled + "<br />";

			if (distanceTravelled > distBetweenWeather) {
				html += '<p class="newPlace">';
				distanceTravelled = 0;
				html += "<strong>Her må vi måle vær. Lat: "
						+ gstep.getLatLng().lat() + ". Lng: "
						+ gstep.getLatLng().lng() + "</strong><br>" + " ";
				html += "<div id='weather" + i + "'> JEG ER EN DIV!!! :O</div>";
				infoObj = new wstep(gstep.getLatLng().lat(), gstep.getLatLng()
						.lng(), j);
				infoTable[countFindings] = infoObj;
				countFindings += 1;
			} else {
				html += '<p class="other">';
			}
			html += "<strong>Beskrivelse</strong>:"
					+ gstep.getDescriptionHtml() + "</p>";
		}
	}
	changeHTMLContent(html);
	setWeather(infoTable);
}

// Metode som setter v�ret etter at tomme spanbokser er satt p� siden.
function setWeather(infoTable) {
	for (i = 0; i < infoTable.length; i++) {
		var infoObj = new wstep(infoTable[i].lat, infoTable[i].lat,
				infoTable[i].id);
		getWeather(infoObj.lat, infoObj.lng, "..", infoObj.id);
	}
}

// Definerer strukturen for et wstep-objekt (weatherstep!)))
function wstep(lat, lng, id) {
	this.lat = lat;
	this.lng = lng;
	this.id = id;
}
